<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="/module/bootstrap-3.4.1/css/bootstrap-theme.min.css">
    <link rel="stylesheet" href="/module/bootstrap-3.4.1/css/bootstrap.min.css">
    <link rel="stylesheet" href="/font/font-awesome-4.7.0/css/font-awesome.css">
    <link rel="stylesheet" href="/css/common.css">
    <title>轮播图</title>
</head>

<body>
    <div id="warp">
        <div class="left">
            <div class="sidebar">
                <div class="navbar navbar-default" id="mycollapse">
                    <!-- <ul class="nav navbar-nav nav-pills nav-stacked"> -->
                    <ul class="nav nav-pills nav-stacked">
                        <li><a href="/index"><i class="fa fa-dashboard fa-fw"></i>首页</a></li>
                        <li><a href="/user"><i class="fa fa-address-card fa-fw"></i>用户</a></li>
                        <li><a href="/subscribe"><i class="fa fa-calendar-times-o fa-fw"></i>预约</a></li>
                        <li><a href="/activity "><i class="fa fa-ship fa-fw"></i>活动</a></li>
                        <li><a href="/integral "><i class="fa fa-line-chart fa-fw"></i>积分</a></li>
                        <li><a href="/opinion"><i class="fa fa-pencil-square fa-fw"></i>意见</a></li>
                        <li><a href="/goods"><i class="fa fa-shopping-cart fa-fw"></i>商品</a></li>
                        <li class="active"><a href="/graph"><i class="fa fa-picture-o fa-fw"></i>轮播图</a></li>
                        <li><a href="/scan"><i class="fa fa-header fa-fw"></i>扫描记录</a></li>
                        <li><a href="/classify"><i class="fa fa-shekel fa-fw"></i>分类</a></li>
                    </ul>
                </div>
            </div>
        </div>
        <div class="right">
            <ol class="breadcrumb">
                <li class="active">轮播图</li>
            </ol>
            <div class="content">
                <!-- 弹窗 -->
                <div class="modal fade" tabindex="-1" role="dialog">
                    <div class="modal-dialog" role="document">
                        <div class="modal-content">
                            <div class="modal-header">
                                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                                        aria-hidden="true">&times;</span></button>
                                <h4 class="modal-title">设置轮播图</h4>
                            </div>
                            <div class="modal-body">
                                <form>
                                    <div class="form-group">
                                        <label for="exampleInputFile">轮播图</label>
                                        <input type="file" id="imgFile">
                                    </div>
                                </form>
                            </div>
                            <div class="modal-footer">
                                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                                <button type="button" class="btn btn-primary" @click="submitClick">提交</button>
                            </div>
                        </div><!-- /.modal-content -->
                    </div><!-- /.modal-dialog -->
                </div><!-- /.modal -->

                <div class="panel panel-default">
                    <!-- Default panel contents -->
                    <div class="panel-heading">轮播图列表</div>
                    <!-- Table -->
                    <table class="table table-bordered table-hover">
                        <tr>
                            <th>序号</th>
                            <th>图片</th>
                            <th>操作</th>
                        </tr>
                        <tr v-for="value in 5" :key="value">
                            <td>{{value}}</td>
                            <td><img :src="graphList[parseInt(value)-1]?.img" alt="..." class="img-rounded" style="width: 50px;height: 50px;"></td>
                            <td>
                                <button type="button" class="btn btn-info btn-xs" @click="setGraph(value)" data-toggle="modal"
                                data-target=".modal">设置</button>
                            </td>
                        </tr>
                    </table>
                </div>
            </div>
        </div>
    </div>

    <script src="/js/jquery.min.js"></script>
    <script src="/module/bootstrap-3.4.1/js/bootstrap.min.js"></script>
    <script src="/js/vue.js"></script>
    <script src="/js/axios.min.js"></script>
    <script>
        const app = new Vue({
            el: '#warp',
            data: {
                graphList: [],
                ordinal: 0,
                img: ''
            },
            methods: {
                getGraph() {
                    axios.get('/request/getGraph').then(res => {
                        if (res.status === 200 && res.data.code === 200) {
                            this.graphList = res.data.graphList
                        } else {
                            alert('请求轮播图失败')
                        }
                    })
                },
                setGraph(index) {
                    this.ordinal = index
                    
                },
                submitClick() {
                    if (this.ordinal !== 0) axios.post('/request/updateGraph',{ordinal: this.ordinal, img: this.img }).then(res => {
                        if (res.status === 200 && res.data.code === 200) {
                            this.getGraph()
                            document.querySelector('#imgFile').value = null
                            $(document.querySelector('.content .modal')).modal('hide')
                            
                        }else {
                            alert('设置轮播图失败')
                        }
                    })
                },
                ToBase64(img) {
                    return new Promise((resolve, reject) => {
                        /*转换base64*/
                        const imgFile = new FileReader();
                        imgFile.readAsDataURL(img.files[0]);
                        imgFile.onload = function () {
                            const imgData = this.result; //base64数据  
                            resolve(imgData)
                        }
                    })
                },
            },
            created() {
                this.getGraph()
            },
            mounted() {
                document.querySelector('#imgFile').onchange = async (e) => {
                    this.img = await this.ToBase64(e.target)
                }
                document.querySelector('#imgFile').onclick = (e) => {
                    e.target.value = null
                }
            },
        })
    </script>
</body>

</html>